package Tests;

import static org.junit.Assert.*;

import javax.swing.DefaultListModel;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Client.PokerClient.src.Client;
import Client.PokerClient.src.GameController;
import Client.PokerClient.src.GameThread;
import Client.PokerClient.src.GameWindow;
import Client.PokerClient.src.ServerController;

public class ServerSTaskControllerTest {
  ServerController serverController;
  Client client;
  static Server server;
  
  @BeforeClass
  public static void BServer() {
    server = new Server(4444);
  }
  
  @AfterClass
  public static void AServer() {
    server.EndTest();
  }
  
  @Before
  public void Inicjuj() {
	client = new Client(true);
	serverController = new ServerController(client);
	client.getGameThread().ConnectToServer("127.0.0.1", "4444");
  }
  
  @Test
  public void S_REFRESH() {
    client.CreateServerWindow();
    client.getGameThread().getServerSTaskController().S_REFRESH("S_REFRESH_5_[111_4/4_2]_[112_2/4_0]_[113_3/4_1]_[1_1/2_0]_[22_2/3_1]");
    
    assertEquals(5, client.getServerController().getID_GAME_LIST().length);
    
    assertEquals("111", client.getServerController().getID_GAME_LIST()[0]);
    assertEquals("112", client.getServerController().getID_GAME_LIST()[1]);
    assertEquals("113", client.getServerController().getID_GAME_LIST()[2]);
    assertEquals("1", client.getServerController().getID_GAME_LIST()[3]);
    assertEquals("22", client.getServerController().getID_GAME_LIST()[4]);
    
    assertTrue(client.getServerController().getServerDatabase().elementAt(0).contains("4/4"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(1).contains("2/4"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(2).contains("3/4"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(3).contains("1/2"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(4).contains("2/3"));
    
    assertTrue(client.getServerController().getServerDatabase().elementAt(0).contains("2"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(1).contains("0"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(2).contains("1"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(3).contains("0"));
    assertTrue(client.getServerController().getServerDatabase().elementAt(4).contains("1"));
  }
  
  @Test
  public void S_ID() {
    client.CreateGame(2, 2, 4, false, true);
    
    client.getGameThread().getServerSTaskController().S_ID("S_ID_5");
    assertEquals("5", client.getGameController().getPlayersID()[0]);
    
    client.getGameThread().getServerSTaskController().S_ID("S_ID_2315");
    assertEquals("2315", client.getGameController().getPlayersID()[0]);
    
    client.getGameThread().getServerSTaskController().S_ID("S_ID_BASDF5");
    assertEquals("BASDF5", client.getGameController().getPlayersID()[0]);
    
    client.getGameThread().getServerSTaskController().S_ID("S_ID_ASDFGH");
    assertEquals("ASDFGH", client.getGameController().getPlayersID()[0]);
    
    client.getGameThread().getServerSTaskController().S_ID("S_ID_asdfasd21");
    assertEquals("asdfasd21", client.getGameController().getPlayersID()[0]);
  }
  
  @Test
  public void S_CREATE() {
	client.CreateGame(2, 2, 4, false, true);
	
	client.getGameThread().getServerSTaskController().S_CREATE("");
    assertEquals("0", client.getGameController().getPlayersID()[0]);
  }
  
  @After
  public void Clean() {
	client.getMainWindow().dispose();
	client.getGameThread().finalize();
	client = null;
	serverController = null;
  }
}
